package com.bdqn.demo01;

import com.bdqn.demo01.dao.NewsDao;
import com.bdqn.demo01.dao.NewsDaoMySQLImpl;
import com.bdqn.demo01.dao.NewsDaoOracleImpl;
import com.bdqn.demo01.dao.NewsDaoRedisImpl;

/**
 * NewDaoFactory 简单工厂（用来创建NewDao接口的实现类）
 *
 * @author LILIBO
 * @since 2022-09-24
 */
public class NewsDaoFactory {

    /**
     * 用来生产NewsDao对象的方法
     *
     * @param key
     * @return
     */
    public static NewsDao getInstance(String key) {
        NewsDao newsDao = null;
        switch (key) {
            case "mysql":
                newsDao = new NewsDaoMySQLImpl();
                break;
            case "oracle":
                newsDao = new NewsDaoOracleImpl();
                break;
            case "redis": // 违背了开闭原则
                newsDao = new NewsDaoRedisImpl();
                break;
            default:
                throw new RuntimeException("无效的数据库类型，DAO实例获取失败。");
        }

        return newsDao;
    }
}
